from django.shortcuts import render
from django.http import HttpResponse
from myapp.models import Pic
import time
from PIL import Image
import os
from django.contrib import messages
from django.shortcuts import redirect




# Create your views here.


def index(request):

	#return HttpResponse("Django")
	return render(request,"index.html")

def pic(request):

	#return HttpResponse("相册")
	# 从数据库中获取数据
	pic_list = Pic.objects.all()
	context = {"pics":pic_list}

	#messages_list = messages.get_messages(request)

	return render(request,"myapp/index.html",context)


def addpic(request):

	return render(request,'myapp/add.html')


def insertpic(request):

	try:
		#print("文件信息：",request.FILES)
		upfile = request.FILES.get("mypic",None)
		#print("文件信息：",upfile.name.split(".").pop())

		if not upfile:
			return HttpResponse("没有上传的文件")

		#print(upfile.name)
		# 随机生成文件名
		filename = str(time.time()) +"." +upfile.name.split(".").pop()
		#print(filename)

		ob = open("./static/"+filename,"wb+")
		for chunk in upfile.chunks():
			ob.write(chunk)

		ob.close()

		# 修改图片大小
		img = Image.open("./static/"+filename)
		img.thumbnail((75, 75))
		# 把缩放后的图像用jpeg格式保存:
		img.save("./static/s_"+filename,None)

		pic = Pic()
		print(pic)
		pic.name = request.POST['name']
		pic.picname = filename
		#pic.addtime = time.time()
		pic.save()
		context = {"info":"上传成功"}
	except Exception as err:
		context = {"info":err}

	return render(request,'myapp/info.html',context)



def delpic(request,uid):

	'''
	执行删除
	'''
	print("uid:",uid)
	try:
		print("there")
		ob = Pic.objects.get(id=uid)
		os.remove("./static/"+ob.picname)
		os.remove("./static/s_"+ob.picname)

		ob.delete()

		# messages.add_message(request, messages.INFO, '删除成功!')
		context = {"info":"删除成功"}

	except Exception as err:
		context = {"info":"删除失败"}
		# messages.add_message(request, messages.INFO, '删除失败!')

	return render(request,'myapp/info.html',context)
	#return redirect('/pic')




def editpic(request,uid):

	try:
		ob = Pic.objects.get(id = uid)
		context = {"pic":ob}
		print("picname是：",context["pic"].picname)
		return render(request,"myapp/edit.html",context)

	except:
		context = {"info":"没有找到要加载的信息！"}
		return render(request,'myapp/info.html',context)


def updatepic(request):

	oldpicname = request.POST["oldname"]
	context = {"info":"拿不到oldpicname"}

	if not oldpicname:
		return render(request,'myapp/info.html',context)

	print("updatepic中拿到的oldpicname:",oldpicname)
	upfile = request.FILES.get("mypic",None)


	# 判断是否上传新的文件
	if not upfile:
		filename = oldpicname
		print("没有上传文件")
		print("filename:",filename)
	else:
		# 执行上传操作

		filename = str(time.time()) +"." +upfile.name.split(".").pop()
		#print(filename)

		ob = open("./static/"+filename,"wb+")
		for chunk in upfile.chunks():
			ob.write(chunk)

		ob.close()

		# 修改图片大小
		img = Image.open("./static/"+filename)
		img.thumbnail((75, 75))
		# 把缩放后的图像用jpeg格式保存:
		img.save("./static/s_"+filename,None)

	pic = Pic()
	pic = Pic.objects.get(id=request.POST['id'])

	pic.name = request.POST['name']
	pic.picname = filename
	#pic.addtime = time.time()
	pic.save()
	context = {"info":"修改成功"}

	# 删除老照片
	if upfile:

		os.remove("./static/"+oldpicname)
		os.remove("./static/s_"+oldpicname)

	return render(request,'myapp/info.html',context)


